from PyQt5.QtWidgets import QVBoxLayout,QHBoxLayout, QLabel, QWidget,QPushButton,QLineEdit,QCheckBox,QGridLayout
from PyQt5.QtWidgets import QFrame
from PyQt5.QtCore import QTimer,Qt
from PyQt5.QtGui import QFont
class DataFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()

        self.show_ui()
    def show_ui(self):
        layout = QVBoxLayout()
        layout.addWidget(QLabel("运行数据页面"))
        self.setLayout(layout)

class ParaFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        self.dic_modbus_h = {}#存储modbus保持寄存器数值
        self.dic_modbus_text = {}#存储modbus线圈寄存器数值
        lst_modbus_h_text = ['过压定值',
            '过压时延',
            '过压报警定值',
            '过压报警时延',
            '欠压定值',
            '欠压时延',
            '欠压报警定值',
            '欠压报警时延',
            '过载定值',
            '过载时延',
            '过载报警定值',
            '过载报警时延',
            '堵转定值',
            '堵转时延',
            '堵转报警定值',
            '堵转报警时延',
            '缺相动作时延',
            '缺相报警时延',
            '失衡定值',
            '失衡时延',
            '失衡报警定值',
            '失衡报警时延',
            '相位错动作时延',
            '相位错报警时延',
            '低温定值',
            '低温动作时延',
            '停温定值',
            '停温时延',
            '低温报警定值',
            '低温报警延时',
            '湿度定值',
            '湿度动作时延',
            '停湿定值',
            '停湿时延',
            '湿度报警定值',
            '湿度报警延时',
            '分闸时间',
            '合闸时间',
            '报警复位',
            '报警复位时间',
            '防抖时间',
            '测试时间',
            '刀闸臂长',
            'RS232_1波特率',
            'RS232_1数据位',
            'RS232_1停止位',
            'RS232_1是否校验',
            'RS232_1协议类型',
            'RS232_2波特率',
            'RS232_2数据位',
            'RS232_2停止位',
            'RS232_2是否校验',
            'RS232_2协议类型',
            'RS485_1波特率',
            'RS485_1数据位',
            'RS485_1停止位',
            'RS485_1是否校验',
            'RS485_1协议类型',
            'RS485_2波特率',
            'RS485_2数据位',
            'RS485_2停止位',
            'RS485_2是否校验',
            'RS485_2协议类型',
            '网口IP地址1',
            '网口IP地址2',
            '网口IP地址3',
            '网口IP地址4',
            '网口子网掩码1',
            '网口子网掩码2',
            '网口子网掩码3',
            '网口子网掩码4',
            '网口网关1',
            '网口网关2',
            '网口网关3',
            '网口网关4',
            '网口服务器IP地址1',
            '网口服务器IP地址2',
            '网口服务器IP地址3',
            '网口服务器IP地址4',
            '网口协议类型',
            '时间设置_年',
            '时间设置_月',
            '时间设置_日',
            '时间设置_时',
            '时间设置_分',
            '时间设置_秒',
            '读取事件记录序号',
            '读取定点记录序号',
            ]
        for i in lst_modbus_h_text:
            self.dic_modbus_h[i] = QLineEdit()
        for i in lst_modbus_h_text:
            self.dic_modbus_text[i] = QLabel(text=i)
        self.dic_modbus_c = {}#存储modbus线圈寄存器数值
        lst_modbus_c_text = ['过压动作',
            '过压报警',
            '欠压动作',
            '欠压报警',
            '过载动作',
            '过载报警',
            '堵转动作',
            '堵转报警',
            '缺相动作',
            '缺相报警',
            '失衡动作',
            '失衡报警',
            '相位错动作',
            '相位错报警',
            '低温动作',
            '低温报警',
            '湿度动作',
            '湿度报警',
            '网口DHCP功能',
                    ]
        for i in lst_modbus_c_text:
            self.dic_modbus_c[i] = QCheckBox(text=i)

        self.initUI()
    def initUI(self):
        layout = QVBoxLayout()
        label = QLabel("参数设置页面")
        #设置label最大宽度和字体,设置居中
        label.setMaximumHeight(50)
        label.setFont(QFont("Arial", 20))
        label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(label)
        self.setLayout(layout)
        frame_lst=["top_frame","mid_frame","bottom_frame"]
        self.dic_frame={}
        for i in frame_lst:
            self.dic_frame[i]=QFrame(self)
            self.dic_frame[i].setFrameShape(QFrame.Shape.Box)
            self.dic_frame[i].setFrameShadow(QFrame.Shadow.Sunken)
            self.dic_frame[i].setLineWidth(2)
            self.dic_frame[i].setMidLineWidth(2)
            layout.addWidget(self.dic_frame[i])
        self.top_frame_layout = QHBoxLayout()
        self.mid_frame_layout = QGridLayout()
        self.bottom_frame_layout = QVBoxLayout()
        self.dic_frame["top_frame"].setLayout(self.top_frame_layout)
        self.dic_frame["mid_frame"].setLayout(self.mid_frame_layout)
        self.dic_frame["bottom_frame"].setLayout(self.bottom_frame_layout)
        self.show_top_frame()
        self.show_mid_frame()
        self.show_bottom_frame()
    def show_top_frame(self):   
        bt1=QPushButton("读取参数",self)
        bt1.clicked.connect(self.read_para)
        bt2=QPushButton("保存参数",self)
        bt2.clicked.connect(self.save_para)
        bt3=QPushButton("登录",self)
        bt3.clicked.connect(self.login)
        self.top_frame_layout.addWidget(bt1)
        self.top_frame_layout.addWidget(bt2)
        self.top_frame_layout.addWidget(bt3)
    def show_mid_frame(self):
        lst_frame=['过压','欠压','过载','堵转','缺相','失衡','相位错','低温','湿度','其他']
        self.dic_mid_frame={}
        for i in lst_frame:
            self.dic_mid_frame[i]=QFrame()
            self.dic_mid_frame[i].setFrameShape(QFrame.Shape.Box)
            self.dic_mid_frame[i].setFrameShadow(QFrame.Shadow.Sunken)
            self.dic_mid_frame[i].setLineWidth(2)
            self.dic_mid_frame[i].setMidLineWidth(2)
            self.mid_frame_layout.addWidget(self.dic_mid_frame[i],lst_frame.index(i)//3,lst_frame.index(i)%3)
        self.dic_mid_frame_layout={}
        for i in lst_frame:
            self.dic_mid_frame_layout[i]=QGridLayout()
            self.dic_mid_frame[i].setLayout(self.dic_mid_frame_layout[i])
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_c["过压动作"],0,0)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_c["过压报警"],0,1)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_text["过压定值"],1,0)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_h["过压定值"],1,1)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_text["过压时延"],1,2)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_h["过压时延"],1,3)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_text["过压报警定值"],2,0)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_h["过压报警定值"],2,1)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_text["过压报警时延"],2,2)
        self.dic_mid_frame_layout["过压"].addWidget(self.dic_modbus_h["过压报警时延"],2,3)        



    def show_bottom_frame(self):
        pass
     

    def read_para(self):
        print("读取参数设置")
    def save_para(self):
        print("保存参数设置")
    def login(self):
        print("登录")

class CalibrationFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("校准执行页面"))
        self.setLayout(layout)

    def read_para(self):
        print("读取校准参数")

class AboutFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        self.main_window=main_window
        layout = QVBoxLayout()
        layout.addWidget(QLabel("关于页面"))
        self.setLayout(layout)
        button = QPushButton("测试提示")
        button.clicked.connect(lambda: self.main_window.set_notice("提示信息","green"))
        layout.addWidget(button)



class StatusFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("状态显示页面"))
        self.setLayout(layout)

class SettingFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("系统设置页面"))
        self.setLayout(layout)

class EventFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("事件记录页面"))
        self.setLayout(layout)

class ControlFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("控制下发页面"))
        self.setLayout(layout)

class CurveFrame(QWidget):
    def __init__(self,main_window=None):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("定点曲线页面"))
        self.setLayout(layout)
